package com.dating.libs.android.view;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ImageSpan;
import android.util.Log;

import com.dating.activity.R;

public class SmileManager {
	Context mContext;
	Map<String, Drawable> mEmoticon=new HashMap<String, Drawable>();
	private SmileManager mInstance;
	public SmileManager(Context context){
		this.mContext=context;
		initSmile();
	}
	public SmileManager getInstance(Context context){
		if(mInstance==null)
			mInstance=new SmileManager(context);
		return mInstance;
	}
	private Drawable getDrawable(int DrawableId){
    	Drawable d=mContext.getResources().getDrawable(DrawableId);
    	d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
    	return d;
    }
	public void initSmile(){
		mEmoticon.put("=P~", 	getDrawable(R.drawable.e_01));
		mEmoticon.put(":-?", 	getDrawable(R.drawable.e_02));
		mEmoticon.put("#-o", 	getDrawable(R.drawable.e_03));
		mEmoticon.put("=D>", 	getDrawable(R.drawable.e_04));
		mEmoticon.put(":-SS", 	getDrawable(R.drawable.e_05));
		mEmoticon.put("@-)", 	getDrawable(R.drawable.e_06));
		mEmoticon.put(":^o", 	getDrawable(R.drawable.e_07));
		mEmoticon.put(":-w", 	getDrawable(R.drawable.e_08));
		mEmoticon.put(":-<", 	getDrawable(R.drawable.e_09));
		mEmoticon.put(">:P", 	getDrawable(R.drawable.e_10));
		mEmoticon.put("<):)", 	getDrawable(R.drawable.e_11));
		
		mEmoticon.put(":)", 	getDrawable(R.drawable.e_37));
    }
	public SpannableStringBuilder convertToSmiley(String s) {
		return convertToSmiley(s, mEmoticon);
	}
    /**@author khacpv
     * @since: 13-5-2012
     * @param: s		text to input
     * @param: ds		list Drawable emoticons (in Drawable folder)
     * @param: smileys	list String	present emoticons ( Ex: :x )
     * @return: SpannableStringBuilder type. To EditText or TextView setText() method.
     * */
    public SpannableStringBuilder convertToSmiley(String s,Map<String, Drawable> mapEmoticons) {
    	SpannableStringBuilder output=new SpannableStringBuilder(s);
    	for(int i=0;i<output.length();++i){
    		//int j=0;
    		for(String smiley: mapEmoticons.keySet()){
    			boolean matching=false;
    			for(int k=0;k<smiley.length();++k){
    				try{
	    				if(smiley.charAt(k)==output.charAt(i+k)){
	    					matching=true;
	    				}else{
	    					matching=false;
	    					break;
	    				}
	    				if(k==smiley.length()-1){
	    					if(matching){
	    						SpannableStringBuilder builder=new SpannableStringBuilder();
	    						builder.append(smiley.toString());
	    						builder.setSpan(new ImageSpan(mapEmoticons.get(smiley)), builder.length()-smiley.length(), builder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
	    						
	    						Log.e("i:"+i+",k:"+k, output.toString());
	    						output.replace(i, i+smiley.length(), builder);
	    					}
    					}
    				}catch (Exception e) {
						break;
					}
    			}
    		}
    	}
    	return output;
    }
}
